# -*- coding:utf-8 -*-
#
# Compatibility module for formatter
# This replaces the deprecated formatter module removed in Python 3.10+

import sys
import textwrap

class DumbWriter:
    """A writer that writes to sys.stdout."""
    
    def __init__(self, file=None):
        self.file = file or sys.stdout
        
    def write(self, data):
        self.file.write(data)
        
    def flush(self):
        self.file.flush()

class AbstractFormatter:
    """A formatter that formats text with word wrapping."""
    
    def __init__(self, writer):
        self.writer = writer
        self.para_end = True
        self.para_space = 0
        self.para_indent = 0
        self.hard_break = 1
        self.soft_break = 0
        self.para_width = 72
        self.max_width = 72
        
    def add_flowing_data(self, data):
        """Add flowing data to the formatter."""
        if not data:
            return
            
        # Simple word wrapping implementation
        words = data.split()
        if not words:
            return
            
        line = ""
        for word in words:
            if len(line) + len(word) + 1 <= self.para_width:
                if line:
                    line += " " + word
                else:
                    line = word
            else:
                if line:
                    self.writer.write(line + "\n")
                line = word
                
        if line:
            self.writer.write(line)
            
    def end_paragraph(self, blankline):
        """End a paragraph."""
        if blankline:
            self.writer.write("\n" * (blankline + 1))
        else:
            self.writer.write("\n")
            
    def add_line_break(self):
        """Add a line break."""
        self.writer.write("\n")
        
    def add_hor_rule(self, *args, **kw):
        """Add a horizontal rule."""
        self.writer.write("-" * self.para_width + "\n")
        
    def add_label_data(self, format, counter):
        """Add label data."""
        pass
        
    def add_whitespace(self, ws):
        """Add whitespace."""
        self.writer.write(" " * ws)